package array;

public class LinkedStack<T> implements StackADT<T> {

    private int count;
    private LinearNode<T> top;
    private LinearNode<T> bottom;

    public LinkedStack() {
        this.top=null;
        this.bottom=null;
        count=0;
    }
    //往链表头插
    @Override
    public void push(T t) {
        LinearNode<T> node = new LinearNode<>(t);
        node.next=top;
        top=node;
        count++;
    }

    @Override
    public T pop() {
        if (isEmpty()) System.out.println("栈空");
        T res=top.data;
        count--;
        top=top.next;
        return res;
    }

    @Override
    public T peek() {
        if (isEmpty()) System.out.println("栈空");

        return top.data;
    }

    @Override
    public boolean isEmpty() {
        return top==null;
    }

    @Override
    public int size() {
        return count;
    }

    @Override
    public void print() {

    }

    public void show(){
        //注意指针一定定义成局部变量
        LinearNode<T> top=this.top;
        while (top!=null){
            System.out.println(top.data);
            top=top.next;
        }
    }

    public static class Test{
        public static void main(String[] args) {
            LinkedStack<Integer> testStack = new LinkedStack<>();
            testStack.push(11);
            testStack.push(22);
            testStack.push(33);
            System.out.println(testStack.pop());
            testStack.show();
            System.out.println(testStack.peek());
            System.out.println(testStack.size());
        }
    }
}
